package com.yingside.jdbc.crm;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class UserDao {
    /**
     * 根据传入的用户名密码确定是否能登录
     * @param username 用户名
     * @param password 密码
     * @return 如果返回值>0,登录成功，否则失败
     */
    public int login(String username,String password){
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        int count = 0;

        try {
            //3.获取连接
            conn = DBHelper.getConn();

            //执行sql语句
            String sql = "select count(*) as count from t_user where username=? and password=?";

            //4.获取PreparedStatement对象
            ps = conn.prepareStatement(sql);

            ps.setString(1,username);
            ps.setString(2,password);


            rs = ps.executeQuery();

            if(rs.next()){
                count = rs.getInt(1);
                //count = rs.getInt("count");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBHelper.close(rs,ps,conn);
        }

        return count;
    }
}
